﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Xml.Xsl;
using System.Xml;
using CANO.Data.DAOs.Usuario;
using CANO.Data.DAOs;

namespace CANO.WebSite.contacts
{
	public partial class ContactsList : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

			if (Session["CurrentUserID"] == null)
				Response.Redirect("/Login.aspx");

			Session["currentModule"] = "Contacts";

			if (!Page.IsPostBack)
			{
				//Set Session variables
				//Session["currentModule"] = "Contacts";
				if (UserHelper.IsManager((Int32)Session["CurrentUserID"]) || UserHelper.IsAsistente((Int32)Session["CurrentUserID"]))
				{
					todosContactosLink.Visible = true;
				}
				else
				{
					btnToXML.Visible = false;
					btnToExcel.Visible = false;
					btnToCSV.Visible = false;
					todosContactosLink.Visible = false;
				}

				this.fillData();
			}
			
			//Bind controls
			btnEditarContact.Click += new EventHandler(btnEditarContact_Click);
			btnVerContact.Click += new EventHandler(btnVerContact_Click);
			todosContactosLink.ServerClick += new EventHandler(todosContactosLink_ServerClick);
			misContactosLink.ServerClick += new EventHandler(misContactosLink_ServerClick);
		}

		void misContactosLink_ServerClick(object sender, EventArgs e)
		{
			this.Store1.DataSource = new Data.DAOs.ContactoDAO().GetContactsByUserID((Int32)Session["CurrentUserID"]);
			this.Store1.DataBind();
			hiddenCurrentTab.Value = "MisContactos";
			
		}

		private void fillData()
		{
			this.Store1.DataSource = new Data.DAOs.ContactoDAO().GetContactsByUserID((Int32)Session["CurrentUserID"]);
			this.Store1.DataBind();
			hiddenCurrentTab.Value = "MisContactos";
		}

		void todosContactosLink_ServerClick(object sender, EventArgs e)
		{
			this.Store1.DataSource = new Data.DAOs.ContactoDAO().GetContacts();
			this.Store1.DataBind();
			hiddenCurrentTab.Value = "TodosLosContactos";

		}
		protected void Cell_Click(object sender, DirectEventArgs e)
		{
			//btnVerContact.Visible = true;
			RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
			Session["RowSelected"] = sm.SelectedRows[0].RecordID;
		}

		protected void ToXml(object sender, EventArgs e)
		{
			string json = GridData.Value.ToString();
			StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
			XmlNode xml = eSubmit.Xml;

			string strXml = xml.OuterXml;

			this.Response.Clear();
			this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
			this.Response.AddHeader("Content-Length", strXml.Length.ToString());
			this.Response.ContentType = "application/xml";
			this.Response.Write(strXml);
			this.Response.End();
		}

		protected void ToExcel(object sender, EventArgs e)
		{
			string json = GridData.Value.ToString();
			StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
			XmlNode xml = eSubmit.Xml;

			this.Response.Clear();
			this.Response.ContentType = "application/vnd.ms-excel";
			this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
			XslCompiledTransform xtExcel = new XslCompiledTransform();
			xtExcel.Load(Server.MapPath("Excel.xsl"));
			xtExcel.Transform(xml, null, this.Response.OutputStream);
			this.Response.End();
		}

		protected void ToCsv(object sender, EventArgs e)
		{
			string json = GridData.Value.ToString();
			StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
			XmlNode xml = eSubmit.Xml;

			this.Response.Clear();
			this.Response.ContentType = "application/octet-stream";
			this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
			XslCompiledTransform xtCsv = new XslCompiledTransform();
			xtCsv.Load(Server.MapPath("Csv.xsl"));
			xtCsv.Transform(xml, null, this.Response.OutputStream);
			this.Response.End();
		}
		
		void btnVerContact_Click(object sender, EventArgs e)
		{
			if (Session["RowSelected"] != null)
			{
				Response.Redirect("/contacts/contact.aspx?ContactID=" + Session["RowSelected"].ToString());

			}
			else
			{
				Response.Redirect("/contacts/contacts.aspx", true);
			}
		}

		void btnEditarContact_Click(object sender, EventArgs e)
		{
			if (Session["RowSelected"] != null)
			{
				Response.Redirect("/contacts/editContact.aspx?ContactID=" + Session["RowSelected"].ToString());
			}
			else
			{
				Response.Redirect("/contacts/contacts.aspx", true);
			}
		}
	}
}